package com.newspublish.system.config;


import com.newspublish.system.custom.CustomMd5Password;
import com.newspublish.system.filter.TokenAuthenticationFilter;
import com.newspublish.system.filter.TokenLoginFilter;
import com.newspublish.system.service.LoginLogService;
//import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;


@Configuration
@EnableWebSecurity //@EnableWebSecurity是开启SpringSecurity的默认行为
@EnableGlobalMethodSecurity(prePostEnabled = true,securedEnabled = true)//开启注解功能，默认禁用注解
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {



    private UserDetailsService userDetailsService;

    private CustomMd5Password customMd5PasswordEncoder;

    private RedisTemplate redisTemplate;

    private LoginLogService loginLogService;

    @Autowired
    public WebSecurityConfig(UserDetailsService userDetailsService, CustomMd5Password customMd5PasswordEncoder,
                             LoginLogService loginLogService, RedisTemplate redisTemplate) {
        this.userDetailsService = userDetailsService;
        this.customMd5PasswordEncoder = customMd5PasswordEncoder;
        this.loginLogService = loginLogService;
        this.redisTemplate = redisTemplate;
    }

    @Override
    protected void configure(@org.jetbrains.annotations.NotNull HttpSecurity http) throws Exception {
        // 这是配置的关键，决定哪些接口开启防护，哪些接口绕过防护
        http.csrf().disable()
                // 开启跨域以便前端调用接口
                .cors()
                .and()
                .authorizeRequests()
//                .antMatchers("/getUser").authenticated()
                .antMatchers("/login").permitAll()
                .anyRequest().authenticated()
                .and()
                .addFilter(new TokenLoginFilter(authenticationManager(), redisTemplate,loginLogService))
                .addFilter(new TokenAuthenticationFilter(authenticationManager(),redisTemplate)).httpBasic();

    }


    //调用userDetailsService和密码处理
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        // 指定UserDetailService和加密器
        auth.userDetailsService(userDetailsService).passwordEncoder(customMd5PasswordEncoder);
    }


    /**
     * 配置哪些请求不拦截
     * 排除swagger相关请求
     * @param web
     * @throws Exception
     */
    @Override
    public void configure(WebSecurity web) throws Exception {
        web.ignoring().antMatchers("/favicon.ico",
                "/swagger-resources/**",
                "/webjars/**", "/v2/**",
                "/swagger-ui.html/**",
                "/doc.html"
//                "/admin/**"
//                "/news/**"
        );
    }
}
